package com.common.supply.config.dynamicdb;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cyh
 * @date 2022/4/29 12:11 下午
 */
@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(DynamicDbConstant.EXT_DATA_SOURCE)
    @ConfigurationProperties(prefix = "spring.datasource.druid.extdata")
    public DataSource extDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(DynamicDbConstant.LINK_CREDIT_SOURCE)
    @ConfigurationProperties(prefix = "spring.datasource.druid.linkcredit")
    public DataSource linkCreditSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(DynamicDbConstant.POWER_KASH_SOURCE)
    @ConfigurationProperties(prefix = "spring.datasource.druid.powerkash")
    public DataSource powerKashSource() {
        return DruidDataSourceBuilder.create().build();
    }


    /**
     * 数据源配置
     *
     * @return
     */
    @Bean
    @Primary
    public DynamicMultipleDataSource multipleDataSource(@Qualifier(DynamicDbConstant.EXT_DATA_SOURCE) DataSource extDataSource,
                                                        @Qualifier(DynamicDbConstant.LINK_CREDIT_SOURCE) DataSource linkCreditSource,
                                                        @Qualifier(DynamicDbConstant.POWER_KASH_SOURCE) DataSource powerKashSource) {
        DynamicMultipleDataSource DynamicMultipleDataSource = new DynamicMultipleDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DynamicDbConstant.EXT_DATA_SOURCE, extDataSource);
        targetDataSources.put(DynamicDbConstant.LINK_CREDIT_SOURCE, linkCreditSource);
        targetDataSources.put(DynamicDbConstant.POWER_KASH_SOURCE, powerKashSource);
        DynamicMultipleDataSource.setTargetDataSources(targetDataSources);
        DynamicMultipleDataSource.setDefaultTargetDataSource(extDataSource);
        return DynamicMultipleDataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory( DynamicMultipleDataSource dynamicMultipleDataSource) throws Exception {
        //SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //不能使用原生的 SqlSessionFactory,换成mybatis的
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dynamicMultipleDataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setLogImpl(StdOutImpl.class);
        mybatisSqlSessionFactoryBean.setConfiguration(configuration);
        return mybatisSqlSessionFactoryBean.getObject();

    }

    @Bean
    public DataSourceTransactionManager transactionManager(DynamicMultipleDataSource DynamicMultipleDataSource) {
        return new DataSourceTransactionManager(DynamicMultipleDataSource);
    }


}
